package com.ruoyi.web.controller.aimp;

import com.ruoyi.aimp.domain.Aimp_Thingwarn;
import com.ruoyi.aimp.service.IAimp_ThingwarnService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 机物告警Controller
 *
 * @author fengfeng
 * @date 2022-06-18
 */
@RestController
@RequestMapping("/ruoyi-aimp/thingwarn")
public class Aimp_ThingwarnController extends BaseController
{
    @Autowired
    private IAimp_ThingwarnService aimp_ThingwarnService;

    /**
     * 查询机物告警列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:list')")
    @GetMapping("/list")
    public TableDataInfo list(Aimp_Thingwarn aimp_Thingwarn)
    {
        startPage();
        List<Aimp_Thingwarn> list = aimp_ThingwarnService.selectAimp_ThingwarnList(aimp_Thingwarn);
        return getDataTable(list);
    }

    /**
     * 导出机物告警列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:export')")
    @Log(title = "机物告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Aimp_Thingwarn aimp_Thingwarn)
    {
        List<Aimp_Thingwarn> list = aimp_ThingwarnService.selectAimp_ThingwarnList(aimp_Thingwarn);
        ExcelUtil<Aimp_Thingwarn> util = new ExcelUtil<Aimp_Thingwarn>(Aimp_Thingwarn.class);
        util.exportExcel(response, list, "机物告警数据");
    }

    /**
     * 获取机物告警详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:query')")
    @GetMapping(value = "/{thingwarnId}")
    public AjaxResult getInfo(@PathVariable("thingwarnId") Long thingwarnId)
    {
        return AjaxResult.success(aimp_ThingwarnService.selectAimp_ThingwarnByThingwarnId(thingwarnId));
    }

    /**
     * 新增机物告警
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:add')")
    @Log(title = "机物告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Aimp_Thingwarn aimp_Thingwarn)
    {
        return toAjax(aimp_ThingwarnService.insertAimp_Thingwarn(aimp_Thingwarn));
    }

    /**
     * 修改机物告警
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:edit')")
    @Log(title = "机物告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Aimp_Thingwarn aimp_Thingwarn)
    {
        return toAjax(aimp_ThingwarnService.updateAimp_Thingwarn(aimp_Thingwarn));
    }

    /**
     * 删除机物告警
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-aimp:thingwarn:remove')")
    @Log(title = "机物告警", businessType = BusinessType.DELETE)
	@DeleteMapping("/{thingwarnIds}")
    public AjaxResult remove(@PathVariable Long[] thingwarnIds)
    {
        return toAjax(aimp_ThingwarnService.deleteAimp_ThingwarnByThingwarnIds(thingwarnIds));
    }

    /**
     * 全景图片上传
     */
    @PostMapping("/upLoadimg") //检测图片服务器路径
    public AjaxResult upLoadimg(@RequestParam("upLoadimgFile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty()){
            //获取文件地址
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 视频上传
     */
    @PostMapping("/upLoadvideo")
    public AjaxResult upLoadvideo(@RequestParam("upLoadpicsFile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty()){
            //获取文件地址
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("videoUrl", avatar);
            return ajax;
        }
        return AjaxResult.error("上传视频异常，请联系管理员");
    }
}
